package cn.bugstack.springframework.test;

import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition2;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory2;
import cn.bugstack.springframework.test.bean.UserService;
import org.junit.Test;

public class ApiTest2 {

    @Test
    public void test_BeanFactory(){
        // 1.初始化 BeanFactory
        DefaultListableBeanFactory2 beanFactory2 = new DefaultListableBeanFactory2();

        // 2.注册 bean
        BeanDefinition2 beanDefinition2 = new BeanDefinition2(UserService.class);
        beanFactory2.registerBeanDefinition("userService",beanDefinition2);

        // 3.第一次获取 bean
        UserService userService = (UserService)beanFactory2.getBean("userService");
        userService.queryUserInfo();

        // 4.第二次获取 bean from Singleton
        UserService userService_singleton = (UserService) beanFactory2.getBean("userService");
        userService_singleton.queryUserInfo();

        System.out.println(userService == userService_singleton);
    }
}
